home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol1 / SerParCIA / SerParResource < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  6.1 KB  |  175 lines

  1. (c)  Copyright 1989-1999 Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice, and 
  3. is provided "as is" without warranty of any kind, either expressed or implied.  
  4. The entire risk as to the use of this information is assumed by the user.
  5.  
  6.  
  7.  
  8.  
  9.                      Using the Serial and 
  10.                     Parallel Port Resources
  11.  
  12.                          by Dan Baker 
  13.                       and Bryce Nesbitt
  14.  
  15.  
  16.  
  17. In some Amiga applications, such as MIDI drivers, the need for high
  18. performance throughput makes direct access to the serial or parallel
  19. hardware desirable.  By going directly to the hardware, the normal overhead
  20. of the serial.device or parallel.device can be eliminated.
  21.  
  22. In order to get direct access to the hardware in a way that is compatible
  23. with multi-tasking, you should use the GetMiscResource() and
  24. FreeMiscResource() routines.  These two routines let you temporarily bar
  25. other tasks from using the resource.  You may then use the associated
  26. hardware directly for your special purposes and then return the resource
  27. back to the system for other tasks to use.
  28.  
  29. GetMiscResource() and FreeMiscResource() are described in Appendix C of the
  30. ROM Kernel Manual: Libraries and Devices.  Since these two routines do not
  31. have a library base pointer name, they can only be called from an assembly
  32. language program.  Here's a working example that gets one of the two 
  33. miscellaneous resources:
  34.  
  35. =======  LAUREN, CODE STARTS HERE!!  ===============================
  36.  
  37. * OPT L+
  38. *
  39. * Assembly language fragment that grabs the two parts of the serial
  40. * resource (using misc.resource).  If it gets the resource, it will
  41. * wait for CTRL-C to be pressed before releasing.
  42. *
  43. * When a task has sucessfully obtained the serial resource, it "owns"
  44. * the hardware registers that control the serial port.  No other tasks
  45. * are allowed to interfere.
  46. *
  47. * This example must be linked with "amiga.lib"
  48. *
  49. *
  50. * Wednesday 07-Dec-88 19:35:13 Bryce Nesbitt
  51. *
  52. *               INCDIR  "inc:"
  53.                 INCLUDE "exec/types.i"
  54.                 INCLUDE "resources/misc.i"
  55.                 INCLUDE "libraries/dos.i"
  56.  
  57. _AbsExecBase    EQU 4
  58.  
  59. JSRLIB  MACRO               ;Macro for easy use of system functions
  60.         XREF    _LVO\1
  61.         JSR     _LVO\1(A6)
  62.         ENDM
  63.  
  64.  
  65.                 move.l  _AbsExecBase,a6 ;Prepare to use exec
  66.                 lea.l   MiscName(pc),a1
  67.                 JSRLIB  OpenResource    ;Open "misc.resource"
  68.                 move.l  d0,d7           ;Stash resource base
  69.                 bne.s   resource_ok
  70.                 moveq   #RETURN_FAIL,d0
  71.                 rts
  72.  
  73. resource_ok     exg.l   d7,a6           ;Put resource base in A6
  74. ;
  75. ; We now have a pointer to a resource.
  76. ; Call one of the resource's library-like vectors.
  77. ;
  78.                 move.l  #MR_SERIALBITS,d0   ;We want these bits
  79.                 lea.l   MyName(pc),a1       ;This is our name
  80.                 jsr     MR_ALLOCMISCRESOURCE(a6)
  81.                 tst.l   d0
  82.                 bne.s   no_bits             ;Someone else has it...
  83.                 move.l  #MR_SERIALPORT,d0
  84.                 lea.l   MyName(pc),a1
  85.                 jsr     MR_ALLOCMISCRESOURCE(a6)
  86.                 tst.l   d0
  87.                 bne.s   no_port             ;Someone else has it...
  88. ;
  89. ; We just stole the serial port registers; wait.
  90. ; Nobody else can use the serial port, including the serial.device!
  91. ;
  92.                 exg.l   d7,a6               ;use exec again
  93.                 move.l  #SIGBREAKF_CTRL_C,d0
  94.                 JSRLIB  Wait                ;Wait for CTRL-C
  95.                 exg.l   d7,a6               ;Get resource base back
  96. ;
  97. ; Free 'em up
  98. ;
  99.                 move.l  #MR_SERIALPORT,d0
  100.                 jsr     MR_FREEMISCRESOURCE(a6)
  101. no_port
  102.                 move.l  #MR_SERIALBITS,d0
  103.                 jsr     MR_FREEMISCRESOURCE(a6)
  104. no_bits
  105.                 moveq   #RETURN_FAIL,d0
  106.                 rts
  107.  
  108. ;
  109. ;text area
  110. ;
  111. MiscName        dc.b    'misc.resource',0
  112. MyName          dc.b    'Serial Port hog',0
  113.                 dc.w    0
  114.                 END
  115.  
  116.  
  117. ==========  LAUREN, CODE ENDS HERE!  ==============
  118.  
  119. Note that there are two serial.device resources to take over, MR_SERIALBITS
  120. and MR_SERIALPORT.  You should get both resources when you take over the
  121. serial port to prevent other tasks from using them.  The parallel.device
  122. also has two resources to take over.  See the resources/misc.h include file
  123. for the relevant C definitions and structures. 
  124.  
  125.  
  126. Under V1.3 and earlier versions of the Amiga system software, there is a bug
  127. in the way the the serial and parallel port resources are handled.  That is,
  128. the GetMiscResource() routine will always fail if the serial.device has
  129. been used at all by another task - even if that task is done using the
  130. resource.  In other words, once a printer driver has been activated, it will
  131. keep the associated resource locked up preventing your task from using it.
  132.  
  133.  
  134. One way around this is to cause a 'memory panic' with the Exec call
  135. AllocMem(0x7FFFFFFFL,0L).  This will cause libraries and resources not
  136. currently in use to be flushed.  However this method has some drawbacks.  It
  137. is unfriendly to other tasks that may be running in the system.  If the user
  138. goes back to a library-based task after a general memory expunge, the user
  139. might be required to reload large libraries from floppy disk.  What is
  140. needed is a less drastic way to get a resource back from a device driver
  141. which has a hold on it.  The code shown below will do this:
  142.  
  143.  
  144.  
  145. ==============  LAUREN, THE REST IS CODE!  ===========
  146.  
  147.   /*
  148.    * A safe way to expunge ONLY a certain device.  The serial.device holds
  149.    * on to the misc serial resource until a general expunge occurs.
  150.    * This code attempts to flush ONLY the named device out of memory and
  151.    * nothing else.  If it fails, no status is returned since it would have
  152.    * no valid use after the Permit().  Code by Bryce Nesbitt.
  153.    */
  154.    #include "exec/types.h"
  155.    #include "exec/execbase.h"
  156.  
  157.    void FlushDevice(char *);
  158.  
  159.    extern struct ExecBase *SysBase;
  160.  
  161.    void FlushDevice(name)
  162.    char  *name;
  163.    {
  164.    struct Device *devpoint;
  165.  
  166.        Forbid();
  167.        if( devpoint=(struct Device *)FindName(&SysBase->DeviceList,name) )
  168.        RemDevice(devpoint);
  169.        Permit();
  170.    }
  171.  
  172.  
  173.  
  174.  
  175.